package com.github.kassadin.kotlinandroid

import android.util.Log
import com.google.gson.Gson
import java.net.URL
import java.nio.charset.Charset

/**
 *
 * Created by <b>kassadin@foxmail.com</b> on 16/1/22 15:43
 */
public class WeatherRequest(val cityid: String) {

    // 伴随对象？
    companion object {
        private val APKKEY = "9935d011c149316c4e5723f43b04dbed"
        private val URL = "http://apis.baidu.com/apistore/weatherservice/recentweathers?cityid="

    }

    public fun execute(): RecentWeathersResp {
        val conn = URL(URL + cityid).openConnection()
        conn.addRequestProperty("apikey", APKKEY)
        val forecastJsonStr = conn.inputStream.use { it.readBytes() }.toString(Charset.defaultCharset())
        Log.d(javaClass.simpleName, forecastJsonStr)
        return Gson().fromJson(forecastJsonStr, RecentWeathersResp::class.java)
    }
}